const app = getApp();
const { AskPage } = app._require("common/index");
const formMixin = app._require("mixins/form.mixin.js").default;
AskPage({
    /**
     * 页面的初始数据
     */
    name: "personal",
    mixins: [formMixin],
    data: {
        url: "/pages/task-home/index",
        isPassText: false,
        model: {
            // phone: "admin",
            // password: "secret",
            phone: "",
            password: "",
        },
    },
    onShow(options){       
        // if (wx.canIUse('hideHomeButton')) {
        //     console.log("ssssss");
        //     wx.hideHomeButton()
        //   }
    },
    onLoad(options) {
       
        this.setData({
            url: options.url || this.data.url,
        });
    },
    onInput(e) {
        // console.log("==================")
        // console.log(e)
        // console.log("==================")
        const value = e.type === "blur" ? e.detail.value : e.detail.value;
        this.setModel({
            currentTarget: e.currentTarget,
            detail: {
                value,
            },
        });
    },
    goBack() {
        
        wx.setStorage({
          data: "1",
          key: 'person_code',
        })
        
        wx.navigateBack(
        );
    },
    async login() {
        let { phone, password } = this.data.model;
    
        let res = this.validateFiled([
            [phone, { required: "请输入手机号!" }],
            [password, { required: "请输入密码!" }],
        ]);
        if (!res.result) {
            wx.showToast({
                title: res.msg,
                icon: "none",
            });
            return false;
        }
        try {
            let { client } = this.data.$_config;
            const response = await this.$_server("user/login", {
                data: {
                    username: phone,
                    password: password,
                },
            });
            let { data } = response;
            if (data.code !== 200 || (data.code === 200 && !data.data.token)) {
                throw data;
            }

            // console.log(data);
            // cityCode  departmentId  governmentIds(不要s取第一个)  orgCategoryId
            data.data.clearManager = true;
            data.data.clearTask = true;
            app.login(data);

            //记录一下需要刷新页面
            wx.setStorageSync('reoladPage', {
                'home':true,
                "note":true
            })

            const eventChannel = this.getOpenerEventChannel();
            try{
                eventChannel.emit("sure");
            }catch{
                console.log(eventChannel);
            }
            this.goBack();
        } catch (error) {
            console.log("error", error);
            // statements
            if (error.message) {
          
                wx.showToast({
                    mask: true,
                    title: error.message,
                    icon: "none",
                    duration: 3000,
                });
                
            } else {
                throw error;
            }
        }
    },
    regiestService(){
        console.log("注册");
        wx.navigateTo({
          url: './regist/index',
        })
    },
    onIconClick(){
        this.setData({
            isPassText: !this.data.isPassText,
        });
    },
    customerService(){
        this.selectComponent('#bottomFrame').showFrame();
    },
    hideFrame(){
        this.selectComponent('#bottomFrame').hideFrame();
    },
    dialNumber(){
        wx.makePhoneCall({
            phoneNumber: '400-8618-890',
            success:function(){
              console.log('拨打成功')
            },
            fail:function(){
              console.log('拨打失败')
            }
          })
        this.selectComponent('#bottomFrame').hideFrame();
    },
    advisory(){
        wx.setClipboardData({
             data:'天作云平台',
             success: function (res) {
               wx.showToast({
                 title: '复制成功',
               });
             }
           });
        this.selectComponent('#bottomFrame').hideFrame();
    },
    onClickForgetPassword(){
         wx.navigateTo({
                url:"/pages/forget-password/index"
            })
    },
     //显示密码
      clickShow(){
           
            this.setData({
              "show_pwd":!this.data.show_pwd
            })
          }
    
});
